//
//  ZZCUserAccount.swift
//  weibo
//
//  Created by zhangzuchao on 17/3/29.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit
//用户信息的存储
let UserAccountPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archive")

//用户的信息, 用于登录以后在欢迎页做展示
class ZZCUserAccount:  NSObject, NSCoding{

    
    //令牌
    var access_token:String?
    
    var expires_in: Int = 0 {
        didSet {
            //  获取当前的时间+过期秒数 = expiresDate
            expiresDate = Date().addingTimeInterval(TimeInterval(expires_in))
        }
    }
    //过期时间
    var expiresDate: Date?
    
    //用户id
    var uid:Int64 = 0
    
    var name: String?
    
    var avatar_large: String?
    
    //  kvc构造函数
    init(dic: [String: Any]) {
        super.init()
        setValuesForKeys(dic)
    }
    //空实现
    override func setValue(_ value: Any?, forUndefinedKey key: String){
    
    }
    
    //用户数据存储  归档与解档
    func encode(with aCoder: NSCoder) {
        
        aCoder.encode(access_token,forKey:"access_token")
        aCoder.encode(expiresDate,forKey:"expiresDate")
        aCoder.encode(uid,forKey:"uid")
        aCoder.encode(name,forKey:"name")
        aCoder.encode(avatar_large,forKey:"avatar_large")
        
        
    }
    //解档方法
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expiresDate = aDecoder.decodeObject(forKey: "expiresDate") as? Date
        uid = aDecoder.decodeInt64(forKey: "uid")
        name = (aDecoder.decodeObject(forKey: "name") as? String)!
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        
    }
    //保存对象
    func saveUserAccount(){
    NSKeyedArchiver.archiveRootObject(self, toFile: UserAccountPath)
    }
    
    //取出对象
    class func loadUserAccount()->ZZCUserAccount?{
    
        let obj = NSKeyedUnarchiver.unarchiveObject(withFile: UserAccountPath)
         return obj as? ZZCUserAccount
    
    }
    
    
    
    
}



























